<html>

	<head>
		<title>My first three.js app</title>
		<style>
			body {
				margin: 0;
			}
			
			canvas {
				width: 100%;
				height: 100%
			}
		</style>
	</head>

	<body>
		<script src="js/three.js"></script>
		<script src="js/js/controls/OrbitControls.js"></script>
		<script src="js/js/objects/Reflector.js"></script>
		<script src="js/js/objects/Refractor.js"></script>
		<script src="js/js/objects/Water2.js"></script>
		<script src="js/js/objects/Flow.js"></script>
		<script src="model/machine.js"></script>
		<script src="model/floor.js"></script>
		<script>
			var scene = new THREE.Scene();
			var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

			var renderer = new THREE.WebGLRenderer();
			renderer.setSize(window.innerWidth, window.innerHeight);
			document.body.appendChild(renderer.domElement);
			var controls = new THREE.OrbitControls(camera, renderer.domElement);
			camera.position.set(30, 30, 1);
			camera.rotation.set(-112, 77, -112);
			controls.update();

			var c_x = 0,
				c_y = 0,
				c_z = 0;

			var machine = new grille_machine(scene);

			camera.position.z = 5;

			var animate = function() {
				requestAnimationFrame(animate);
				setCamera();
				renderer.render(scene, camera);
			};

			//			initGrid();
			initFloor();
			animate();

			function initGrid() {
				var size = 500
				var divisions = 100;
				var gridHelper = new THREE.GridHelper(size, divisions);
				scene.add(gridHelper);
			}

			function initFloor() {
				var floor_texture = new THREE.TextureLoader().load('textures/floor.png', function(map) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 11;
					map.repeat.set(50, 50);
					floor_texture.map = map;
					floor_texture.needsUpdate = true;
				});
				var floor_material = new THREE.MeshBasicMaterial({
					map: floor_texture
				});
				var floor = new THREE.BoxGeometry(250, 1, 250);
				var floor_mesh = new THREE.Mesh(floor, floor_material);
				floor_mesh.position.y = 0;
				scene.add(floor_mesh);

			}

			function setCamera() {
				if(camera.position.y < 10) {
					camera.position.y = 10;
					camera.position.z = c_z;
					camera.position.x = c_x;
					controls.update();
					return;
				}
				c_x = camera.position.x;
				c_y = camera.position.y;
				c_z = camera.position.z;
				controls.update();
			}
		</script>
	</body>

</html>